package com.toommi.refuse.service.impl;

import com.toommi.refuse.entity.WeightGather;
import com.toommi.refuse.entity.WeightGatherExample;
import com.toommi.refuse.mapper.WeightGatherMapper;
import com.toommi.refuse.model.WeightGatherModel;
import com.toommi.refuse.service.WeightGatherService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangg
 * @since 2019-08-05 17:54
 */
@Service
@AllArgsConstructor
public class WeightGatherServiceImpl implements WeightGatherService {

    private WeightGatherMapper gatherMapper;


    @Override
    public void addWeightGather(WeightGatherModel model) {

        List<WeightGather> weightGathers = model.getList();
        if (weightGathers == null || weightGathers.isEmpty()) {
            return;
        }
        //先判断今天是否已经汇总过了

        for (WeightGather weightGather : weightGathers) {
            weightGather.setAppUserId(model.getAppUserId());
            weightGather.setDate(model.getDate());
            weightGather.setCreateTime(System.currentTimeMillis());
            weightGather.setUpdateTime(System.currentTimeMillis());
            gatherMapper.insert(weightGather);
        }

    }

    @Override
    public WeightGatherModel findWeightGather(long appUserId, String date) {

        WeightGatherExample example = new WeightGatherExample();
        example.createCriteria().andAppUserIdEqualTo(appUserId).andDateEqualTo(date);

        List<WeightGather> weightGatherList = gatherMapper.selectByExample(example);

        WeightGatherModel model = new WeightGatherModel();

        if (weightGatherList == null || weightGatherList.isEmpty()) {
            model.setList(new ArrayList<>());
        } else {
            model.setList(weightGatherList);
        }
        model.setAppUserId(appUserId);
        model.setDate(date);
        return model;
    }
}
